home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / jovept2.arc / TEMP.C < prev    next >
Text File  |  1985-05-30  |  5KB  |  249 lines

  1. /* temp.c */
  2.  
  3. /* JOVE/MSDOS. K. Mitchum 1/85 */
  4. /* Modifications for personal use only. */
  5. /* original code J. Payne LSRHS 5/83 */
  6. /* Ken Mitchum */
  7. /* University of Pittsburgh */
  8. /* Decision Systems Laboratory */
  9.  
  10. /*
  11.    Jonathan Payne at Lincoln-Sudbury Regional High School 5-25-83
  12.   
  13.    Much of this code was lifted from VI (ex_temp.c).  These functions
  14.    deal with (put/get)ing lines in/from the tmp file.  */
  15.  
  16. #define JOVETEMP
  17.  
  18. #include "jove.h"
  19. #include "temp.h"
  20. #include "files.h"
  21.  
  22.  
  23. int    DOLsave = 0;    /* Do Lsave flag.  If lines aren't being save
  24.                when you think they should have been, this
  25.                flag is probably not being set, or is being
  26.                cleared before lsave() was called. */
  27.  
  28.  
  29. tmpinit()
  30. {
  31. #ifdef UNIX
  32.     tfname = mktemp(TMPFILE);
  33. #else
  34.     tfname = "joveswap";
  35. #endif
  36.     tline = 2;
  37.     iblock1 = oblock = iblock2 = -1;
  38.     hitin2 = ichng1 = ichng2 = 0;
  39.     ignore(close(creat(tfname, TEMPMODE)));
  40.     tmpfd = open(tfname, RWMODE);
  41.     if (tmpfd == -1) {
  42.         putstr(sprint("%s?\n", tfname));
  43.         finish(0);
  44.     }
  45. }
  46. #ifndef UNIX
  47. closetmp()
  48. {
  49.     close(tmpfd);
  50. }
  51. #endif
  52.  
  53. /* Get a line at `tl' in the tmp file into `buf' which should be LBSIZE
  54.    long. */
  55.  
  56. char *
  57. getline(tl, buf)
  58. disk_line    tl;
  59. char    *buf;
  60. {
  61.     register char    *bp,
  62.             *lp;
  63.     register int    nl;
  64.  
  65.     lp = buf;
  66.     bp = getblock(tl, READ);
  67.     nl = nleft;
  68.     tl &= ~OFFMSK;
  69.     while (*lp++ = *bp++) {
  70.         if (--nl == 0) {
  71.             /* += INCRMT moves tl to the next block in
  72.                the tmp file. */
  73.             bp = getblock(tl += INCRMT, READ);
  74.             nl = nleft;
  75.         }
  76.     }
  77.  
  78.     return buf;
  79. }
  80.  
  81. /* Put `buf' and return the disk address */
  82.  
  83. disk_line
  84. putline(buf)
  85. char    *buf;
  86. {
  87.     register char    *bp,
  88.             *lp;
  89.     register disk_line    nl;
  90.     disk_line    tl;
  91.  
  92.     lp = buf;
  93.     tl = tline;
  94.     bp = getblock(tl, WRITE);
  95.     nl = nleft;
  96.     tl &= ~OFFMSK;
  97.     while (*bp = *lp++) {
  98.         if (*bp++ == '\n') {
  99.             *--bp = 0;
  100.             break;
  101.         }
  102.         if (--nl == 0) {
  103.             bp = getblock(tl += INCRMT, WRITE);
  104.             nl = nleft;
  105.         }
  106.     }
  107.     nl = tline;
  108.     tline += (((lp - buf) + BNDRY - 1) >> SHFT) & 077776;
  109.     return nl | DIRTY;    /* So it will be redisplayed */
  110. }
  111.  
  112. /* Get a block which contains at least part of the line with the address
  113.  * atl.  Returns a pointer to the block and sets the global variable
  114.  * nleft (number of good characters left in the buffer)
  115.  */
  116.  
  117. char *
  118. getblock(atl, iof)
  119. disk_line    atl;
  120. {
  121.     extern int    read(),
  122.             write();
  123.     register int    bno,
  124.             off;
  125.  
  126.     if (atl == 0)
  127.         complain("Free line in list!!!");
  128.     bno = (atl >> OFFBTS) & BLKMSK;
  129.     off = (atl << SHFT) & LBTMSK;
  130.     if (bno >= NMBLKS)
  131.         error("Tmp file too large.  Get help");
  132.     nleft = BUFSIZ - off;
  133.     if (bno == iblock1) {
  134.         ichng1 |= iof;
  135.         return ibuff1 + off;
  136.     }
  137.     if (bno == iblock2) {
  138.         ichng2 |= iof;
  139.         return ibuff2 + off;
  140.     }
  141.     if (bno == oblock)
  142.         return obuff + off;
  143.     if (iof == READ) {
  144.         if (hitin2 == 0) {
  145.             if (ichng2)
  146.                 blkio(iblock2, ibuff2, write);
  147.             ichng2 = 0;
  148.             iblock2 = bno;
  149.             blkio(bno, ibuff2, read);
  150.             hitin2 = 1;
  151.             return (ibuff2 + off);
  152.         }
  153.         hitin2 = 0;
  154.         if (ichng1)
  155.             blkio(iblock1, ibuff1, write);
  156.         ichng1 = 0;
  157.         iblock1 = bno;
  158.         blkio(bno, ibuff1, read);
  159.         return (ibuff1 + off);
  160.     }
  161.     if (oblock >= 0)
  162.         blkio(oblock, obuff, write);
  163.     oblock = bno;
  164.     return obuff + off;
  165. }
  166.  
  167. #ifdef    VMUNIX
  168. #define    INCORB    64
  169. char    incorb[INCORB+1][BUFSIZ];
  170. #define    pagrnd(a)    ((char *)(((int)a)&~(BUFSIZ-1)))
  171. #endif
  172.  
  173. blkio(b, buf, iofcn)
  174. short    b;
  175. char    *buf;
  176. int    (*iofcn)();
  177. {
  178.  
  179. #ifdef VMUNIX
  180.     if (b < INCORB) {
  181.         if (iofcn == read) {
  182.             bcopy(pagrnd(incorb[b+1]), buf, BUFSIZ);
  183.             return;
  184.         }
  185.         bcopy(buf, pagrnd(incorb[b+1]), BUFSIZ);
  186.         return;
  187.     }
  188. #endif
  189.     ignore(lseek(tmpfd, (long) (unsigned) b * BUFSIZ, 0));
  190.     if ((*iofcn)(tmpfd, buf, BUFSIZ) != BUFSIZ)
  191.         error("IO error");
  192. }
  193.  
  194. #ifdef VMUNIX
  195.  
  196. /* block copy from from to to, count bytes */
  197.  
  198. bcopy(from, to, count)
  199. #ifdef vax
  200.     char *from, *to;
  201.     int count;
  202. {
  203.  
  204.     asm("    movc3    12(ap),*4(ap),*8(ap)");
  205. }
  206. #else
  207.     register char *from, *to;
  208.     register int count;
  209. {
  210.     while ((--count) >= 0)
  211.         *to++ = *from++;
  212. }
  213. #endif
  214. #endif
  215.  
  216. /*
  217.  * Save the current contents of linebuf, if it has changed.
  218.  */
  219.  
  220. lsave()
  221. {
  222.     char    tmp[LBSIZE];
  223.  
  224.     if (curbuf == 0 || !DOLsave)    /* Nothing modified recently */
  225.         return;
  226.  
  227.     if (!strcmp(linebuf, getline(curline->l_dline, tmp)))
  228.         return;        /* They are the same. */
  229.     SavLine(curline, linebuf);    /* Put linebuf on the disk */
  230.     DOLsave = 0;
  231. }
  232.  
  233. getDOT()
  234. {
  235.     ignore(getline(curline->l_dline, linebuf));
  236. }
  237.  
  238. SavLine(addr, buf)
  239. LINE    *addr;
  240. char    *buf;
  241. {
  242.     addr->l_dline = putline(buf);
  243. }
  244.  
  245.  
  246. /*----------------------------------o.s. dependent-------------------*/
  247.  
  248. /* end */
  249.